package com.kedll.library.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.kedll.library.R;


/**
 * 对话框基类
 *
 * @author QiQi
 * @date 2016/10/29
 */
public abstract class KBaseDialogFragment extends DialogFragment {
    protected String sTitle;
    protected String sMsg;
    protected String hint;
    protected DialogInterface.OnCancelListener onCancelListener;
    protected DialogInterface.OnDismissListener onDismissListener;
    private View rootView;

    public KBaseDialogFragment setHint(String hint) {
        this.hint = hint;
        return this;
    }

    public KBaseDialogFragment setTitle(String title) {
        this.sTitle = title;
        return this;
    }

    public KBaseDialogFragment setMsg(String msg) {
        this.sMsg = msg;
        return this;
    }

    public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
        this.onCancelListener = onCancelListener;
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        this.onDismissListener = onDismissListener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_FRAME, R.style.my_dialog);
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        if (onCancelListener != null) {
            onCancelListener.onCancel(dialog);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        if (onDismissListener != null) {
            onDismissListener.onDismiss(dialog);
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData(savedInstanceState);
        initEvent();

        loadData(getArguments());
    }

    protected abstract void initData(Bundle savedInstanceState);

    protected abstract void initEvent();

    protected abstract void loadData(Bundle arguments);

    public <T extends View> T findViewById(@IdRes int id) {
        return rootView.findViewById(id);
    }

    /**
     * showDialog
     */
    public void showDialog(FragmentManager manager, String tag) {
        showDialog(manager, null, tag);
    }

    /**
     * showDialog
     */
    public void showDialog(FragmentManager manager, Bundle bundle, String tag) {
        FragmentTransaction mTransaction = manager.beginTransaction();
        if (manager.findFragmentByTag(tag) != null) {
            mTransaction.remove(manager.findFragmentByTag(tag));
        }
        if (bundle != null) {
            setArguments(bundle);
        }
        show(manager, tag);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        try {
            if (rootView == null) {
                rootView = inflater.inflate(getLayout(), container, false);
            }

            ViewGroup parent = (ViewGroup) rootView.getParent();
            if (parent != null) {
                parent.removeView(rootView);
            }
            initView(rootView);
            return rootView;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    protected abstract int getLayout();

    protected abstract void initView(View rootView);
}
